//
//  ApplyTableViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ApplyTableViewController.h"

#import "ApplyFriendCell.h"


@interface ApplyTableViewController ()<ApplyFriendCellDelegate>

@end

@implementation ApplyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.title = @"好友申请";
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [backButton setImage:[UIImage imageNamed:@"back@2x.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    [self.tableView registerClass:[ApplyFriendCell class] forCellReuseIdentifier:@"cell"];
    
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [CCContactTool shareContactTool].inviteUserInfoDic.count;
}


- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ApplyFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  
    
    cell.delegate = self;
    
    
    cell.textLabel.text = [[CCContactTool shareContactTool].arrayOfUsers objectAtIndex:indexPath.row];
    
    
    
//    cell.contentLabel.text = [[CCContactTool shareContactTool].messagesArray objectAtIndex:indexPath.row];
    
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 一定要注意是这个方法取出当前的cell*******&&&&&&&*******&&&&&&&^^%%^%^$$##@@!@
    ApplyFriendCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.indexPath = indexPath;
    
    
}




// ApplyFriendCell的协议方法
- (void)applyCellAddFriendAtIndexPath:(NSIndexPath *)indexPath{
    
    
    // 接受好友申请
    
    NSString *userName = [[CCContactTool shareContactTool].arrayOfUsers objectAtIndex:indexPath.row];
    
    
    EMError *error = [[EMClient sharedClient].contactManager acceptInvitationForUsername:userName];
    
    if (!error) {
        
        [[CCContactTool shareContactTool] removeUser:userName];
        
        [self.tableView reloadData];
    }
    
}
- (void)applyCellRefuseFriendAtIndexPath:(NSIndexPath *)indexPath{

     // 拒绝好友申请
      NSString *userName = [[CCContactTool shareContactTool].arrayOfUsers objectAtIndex:indexPath.row];
    
    EMError *error = [[EMClient sharedClient].contactManager declineInvitationForUsername:userName];
    if (!error) {
        
        [[CCContactTool shareContactTool] removeUser:userName];
        
        [self.tableView reloadData];
        
        
    }else{
        
        
        NSLog(@"%@", error.description);
        
    }
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//        ApplyEntity *entity = [self.dataSource objectAtIndex:indexPath.row];
//        return [ApplyFriendCell heightWithContent:entity.reason];
    
    return 100;
}






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
